package com.ning.ems.controller;

import com.ning.ems.dto.AddressAddDto;
import com.ning.ems.dto.AddressUpdateDto;
import com.ning.ems.entity.Address;
import com.ning.ems.service.AddressService;
import com.ning.ems.vo.AddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Ning 242741154@qq.com
 * @date 2020/4/12 1:50
 */
@Api(tags = "地址管理相关接口")
@RestController
@RequestMapping("/addr")
public class AddressController {
    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @ApiOperation("地址列表")
    @GetMapping("/list")
    private List<AddressVo> list(@RequestParam(value = "ownerId") Integer ownerId) {
        return addressService.list(ownerId);
    }

    @ApiOperation("通过 id 获取地址")
    @GetMapping("/get")
    private AddressVo get(@RequestParam(value = "id") Integer id) {
        return addressService.get(id);
    }

    @ApiOperation("通过 id 获取地址描述")
    @GetMapping("/desc")
    private String desc(@RequestParam(value = "id") Integer id) {
        return addressService.desc(id);
    }

    @ApiOperation("添加地址")
    @PostMapping("/add")
    private AddressVo add(@RequestBody @Valid AddressAddDto info) {
        Address addr = new Address();
        addr.setOwner(info.getOwner());
        addr.setContactNumber(info.getContactNumber());
        addr.setProvince(info.getProvince());
        addr.setExactAddr(info.getExactAddr());
        return addressService.add(addr);
    }

    @ApiOperation("通过 id 删除地址")
    @DeleteMapping("/delete")
    private boolean delete(@RequestParam(value = "id") Integer id) {
        return addressService.delete(id);
    }

    @ApiOperation("更改地址信息")
    @PutMapping("/update")
    private boolean update(@RequestBody @Valid AddressUpdateDto info) {
        return addressService.update(info);
    }
}
